//
//  VIZWelcomeVC.swift
//  VIZ微博
//
//  Created by 位立志 on 16/4/11.
//  Copyright © 2016年 位立志. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class VIZWelcomeVC: UIViewController {

    
    override func loadView() {
        
        view = UIImageView(image: UIImage(named: "ad_background"))
    }
    
    
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)

    }
    
    
    //懒加载控件
    lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    lazy var nameLabel:UILabel = {
        let l = UILabel()
        l.text = VIZUserViewAccountModel.shardInstance.userInfo?.name
        l.font = UIFont.systemFontOfSize(14)
        l.textColor = UIColor.blackColor()
        return l
    }()
 //   lazy var nameLabel:UILabel = UILabel(text: (VIZUserViewAccountModel.shardInstance.userInfo?.name)!, textColor: UIColor.blackColor(), fontSize: 14)
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(iconImageView)
        view.addSubview(nameLabel)
        self.setUI()
        
        //使用SBWebImage从网络上获取头像
        iconImageView.sd_setImageWithURL(VIZUserViewAccountModel.shardInstance.iconImageUrl)
        //美化
        iconImageView.cornerRadius = 45;
        //让nameLabel不显示
        nameLabel.alpha = 0
        
    }

    //设置frame
    func setUI() {
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-180)
            //设置头像大小
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconImageView.snp_centerX)
            
            make.top.equalTo(iconImageView.snp_bottom).offset(15)
        }

        
    }
    
    
    //重写viewDidAppera方法
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //开始动画
        self.startAnimation()
        
    }
    
    
    //添加动画
    func startAnimation() {
        
        //更新约束
        self.iconImageView.snp_updateConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.view.snp_centerX)
            
            make.bottom.equalTo(self.view.snp_bottom).offset(-380)
        }
        
        //设置动画

        UIView.animateWithDuration(1.5, delay: 0.5, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            UIView.animateWithDuration(1.0, animations: { () -> Void in
                self.nameLabel.alpha = 1
            })
            }) { (_) -> Void in
                NSNotificationCenter.defaultCenter().postNotificationName(VIZchange, object: "main")
        }
        
    }
    
    
    
    
    
    
    
    
    
}
